using System;
using System.IO;
using System.Collections.Generic;

namespace MinesweeperPlus.Scripts.Objects
{
    public sealed class GameData
    {
        #region Constants
        private const int DEFAULT_MONEY_VALUE = 100;            
        private const int MIN_MONEY_VALUE = 0;
        private const int MAX_MONEY_VALUE = 999999;

        private const string DEFAULT_SELECTED_COLOR_SCHEME_NAME = "Default Light";            
        private const string DEFAULT_BOUGHT_COLOR_SCHEME_NAME_0 = DEFAULT_SELECTED_COLOR_SCHEME_NAME;            
        private const string DEFAULT_BOUGHT_COLOR_SCHEME_NAME_1 = "Default Dark";

        #endregion

        #region Actions
        public Action<int> OnMoneyValueChange;
        #endregion

        #region Variables
        private string _filePath;
        private int _money;
        private string _selectedColorSchemeName;
        #endregion
        #region Properties
        public int Money 
        { 
            get => _money; 
            set
            {
                if (_money == value)
                    return;
                
                _money = value;
                OnMoneyValueChange?.Invoke(_money);
                Save();
            }
        }
        
        public string SelectedColorSchemeName 
        { 
            get => _selectedColorSchemeName; 
            set
            {
                if (_selectedColorSchemeName == value)
                    return;

                _selectedColorSchemeName = value;
                Save();
            }
        }
        public List<string> BoughtColorSchemeNames { get; private set; }
        #endregion

        #region  GameData Methods
        public GameData(string filePath)
        {
            _filePath = filePath;

            _money = DEFAULT_MONEY_VALUE;
            _selectedColorSchemeName = DEFAULT_SELECTED_COLOR_SCHEME_NAME;

            BoughtColorSchemeNames = new();
            BoughtColorSchemeNames.Add(DEFAULT_BOUGHT_COLOR_SCHEME_NAME_0);
            BoughtColorSchemeNames.Add(DEFAULT_BOUGHT_COLOR_SCHEME_NAME_1);

            if (!File.Exists(filePath))
                Save();
            else
                Load();
        }
        #endregion

        #region Other Methods
        private void Load()
        {
            using (var reader = new BinaryReader(File.OpenRead(_filePath)))
            {
                _money = reader.ReadInt32();
                _selectedColorSchemeName = reader.ReadString();

                BoughtColorSchemeNames.Clear();
                try
                {
                    while (true)
                        BoughtColorSchemeNames.Add(reader.ReadString());
                }
                catch { }
            }
        }
        private void Save()
        {
            using (var writer = new BinaryWriter(File.OpenWrite(_filePath)))
            {
                writer.Write(_money);
                writer.Write(_selectedColorSchemeName);

                foreach (var boughtColorSchemeName in BoughtColorSchemeNames)
                    writer.Write(boughtColorSchemeName);
            }
        }
        #endregion
    }
}
